package com.hwpt.hwaccount.feature.security;

import com.hwpt.hwaccount.core.data.local.entity.UserEntity;

import java.lang.ref.WeakReference;

public class SecurityPresenter implements SecurityContract.Presenter {
    private WeakReference<SecurityContract.View> viewRef;
    private final SecurityContract.Model model;
    private UserEntity currentUser;

    public SecurityPresenter(SecurityContract.Model model) {
        this.model = model;
    }

    @Override
    public void attachView(SecurityContract.View view) {
        this.viewRef = new WeakReference<>(view);
    }

    @Override
    public void detachView() {
        if (viewRef != null) {
            viewRef.clear();
        }
    }

    @Override
    public void onDestroy() {
        if (model instanceof SecurityModel) {
            ((SecurityModel) model).dispose();
        }
        detachView();
    }

    @Override
    public void loadSecurityInfo() {
        SecurityContract.View view = viewRef.get();

        model.getUserInfo(new SecurityContract.Model.DataCallback<UserEntity>() {
            @Override
            public void onSuccess(UserEntity data) {
                currentUser = data;
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSecurityInfo(data);
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("加载失败: " + message);
                }
            }
        });
    }

    @Override
    public void changePassword(String currentPassword, String newPassword) {
        SecurityContract.View view = viewRef.get();


        model.changePassword(currentPassword, newPassword, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("密码修改成功");
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("密码修改失败: " + message);
                }
            }
        });
    }

    @Override
    public void setupEmergencyContact(String name, String phone, String relationship) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        SecurityContract.View view = viewRef.get();

        model.setupEmergencyContact(name, phone, relationship, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("紧急联系人设置成功");
                    loadSecurityInfo();
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("紧急联系人设置失败: " + message);
                }
            }
        });
    }

    @Override
    public void sendPhoneVerifyCode(String phone) {
        SecurityContract.View view = viewRef.get();
        if (view != null) {
        }

        model.sendPhoneVerifyCode(phone, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("验证码已发送");
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("验证码发送失败: " + message);
                }
            }
        });
    }

    @Override
    public void bindPhone(String phone, String code) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        SecurityContract.View view = viewRef.get();

        model.bindPhone(phone, code, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("手机号绑定成功");
                    loadSecurityInfo();
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("手机号绑定失败: " + message);
                }
            }
        });
    }

    @Override
    public void unbindPhone() {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        SecurityContract.View view = viewRef.get();
        if (view != null) {
        }

        model.unbindPhone(new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("手机号解绑成功");
                    loadSecurityInfo();
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("手机号解绑失败: " + message);
                }
            }
        });
    }

    @Override
    public void sendEmailVerifyCode(String email) {
        SecurityContract.View view = viewRef.get();

        model.sendEmailVerifyCode(email, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("验证码已发送");
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("验证码发送失败: " + message);
                }
            }
        });
    }

    @Override
    public void bindEmail(String email, String code) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        SecurityContract.View view = viewRef.get();

        model.bindEmail(email, code, new SecurityContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("邮箱绑定成功");
                    loadSecurityInfo();
                }
            }

            @Override
            public void onError(String message) {
                SecurityContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("邮箱绑定失败: " + message);
                }
            }
        });
    }

    private void showError(String message) {
        SecurityContract.View view = viewRef.get();
        if (view != null) {
            view.showError(message);
        }
    }
}